package com.zyx.service;

import com.zyx.mapper.NameMapper;
import com.zyx.pojo.Auto;
import com.zyx.pojo.Name;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class NameService {
    @Resource
    private NameMapper nameMapper;

    //查询车辆品牌
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<String> queryByFirm() {
        return nameMapper.queryByFirm();
    }

    //查询汽车名称和id
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Name> queryByAutoId(String firm) {
        return nameMapper.queryByAutoId(firm);
    }

    //查询汽车型号和id
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Name> queryByModel(Integer id) {
        return nameMapper.queryByModel(id);
    }

    //查询汽车型号和id
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Name> queryById(Integer id) {
        return nameMapper.queryById(id);
    }

    //根据车型id查询车辆信息
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Name queryName(Integer id) {
        return nameMapper.selectByPrimaryKey(id);
    }

    //根据车型id添加图片
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int carNameLogo(Name name) {
        return nameMapper.updateByPrimaryKeySelective(name);
    }

    //根据条件查询车型信息
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Name> queryByName(Auto auto) {
        return nameMapper.queryByName(auto);
    }

    //根据无条件查询车型信息
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Name> queryByNull() {
        return nameMapper.queryByNull();
    }

    //添加新的车型
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addAutoName(Name name) {
        int i = 0;
        try {
            i = nameMapper.insertSelective(name);
        } catch (Exception e) {
            if (e.getMessage().endsWith("for key 'name.carName'")) {
                i = -1;
            } else {
                e.printStackTrace();
            }
        }
        return i;
    }
}
